package com.hm.health.Utils;

import android.content.Context;
import android.util.Log;

import com.hm.health.entity.BleDevicesInfo;
import com.hm.health.entity.HeartrateData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * Created by jiaguang on 2018/11/7.
 */

public class HciFileHeartRateUtil {
    /**
     * 将HCI文件中的心率信息合并到心率历史数据中
     */

    private static final String TAG = "HciFileHeartRateUtil";

    private static SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    private static Context mContext;
    private static boolean mIsCombined = false;

    public static void addHciHeartRate(ArrayList<HeartrateData> mDataList, long startTimeSecond,
                                       ArrayList<HeartrateData> mDataListDelay, long startTimeDelaySecond, Context context, HciFileHeartRateFetcher.HciCombineCallback hciCombineCallback){
        mContext = context;
        mIsCombined = false;
        HciFileHeartRateFetcher hciFileHeartRateFetcher = new HciFileHeartRateFetcher(hciCombineCallback, context);
        LinkedHashMap<Long, Integer> timeAndHeartRateMap = hciFileHeartRateFetcher.getAndSaveTimeAndHeartRate();
        if(null != timeAndHeartRateMap){
            addHeartRate(mDataList, startTimeSecond, timeAndHeartRateMap);
            addHeartRate(mDataListDelay, startTimeDelaySecond, timeAndHeartRateMap);
            if(!mIsCombined){
                onCombineResult(HciFileHeartRateFetcher.HCI_FILE_COMBINING_FAILED, hciCombineCallback);
            } else {
                onCombineResult(HciFileHeartRateFetcher.HCI_FILE_COMBINING_SUCCESS, hciCombineCallback);
            }
        } else {
            Log.d(TAG, "getAndSaveTimeAndHeartRate failed");
        }
    }

    private static void addHeartRate(ArrayList<HeartrateData> mDataList, long startTimeSecond, HashMap<Long, Integer> timeAndHeartRateMap){
        int dataListIndex = 0;
        long dataListTimeSecond = startTimeSecond;
        long hciTime;
        int hciHeartRate;
        Set<Long> keySet = timeAndHeartRateMap.keySet();
        Iterator<Long> keyIterator = keySet.iterator();
        Log.d(TAG, "keySet size: " + keySet.size());
        while(keyIterator.hasNext() && dataListIndex < mDataList.size()){
            hciTime = keyIterator.next();
            while(hciTime / 1000 >= dataListTimeSecond && dataListIndex < mDataList.size()){
                hciHeartRate = 0;
                if(hciTime / 1000 == dataListTimeSecond){
                    hciHeartRate = timeAndHeartRateMap.get(hciTime);
                    mIsCombined = true;
                }
                Log.d(TAG, "hciTime: " + hciTime + ", dataListTimeSecond: " + dataListTimeSecond +
                        ", hciTime - dataListTimeSecond = " + (hciTime - dataListTimeSecond) + ",  hciHeartRate: " + hciHeartRate);
                BleDevicesInfo bleDevicesInfo = new BleDevicesInfo(HciFileHeartRateFetcher.getHciDevice(mContext), null);
                bleDevicesInfo.setValue(hciHeartRate);
                mDataList.get(dataListIndex).mBleDeviceHeartRateList.add(bleDevicesInfo);//
                dataListIndex++;
                dataListTimeSecond = startTimeSecond + dataListIndex;
            }
            Log.d(TAG, "hciTime: " + mSimpleDateFormat.format(hciTime) + ", dataListTimeSecond: " + mSimpleDateFormat.format(dataListTimeSecond));
        }
        while(dataListIndex < mDataList.size()){
            BleDevicesInfo bleDevicesInfo = new BleDevicesInfo(HciFileHeartRateFetcher.getHciDevice(mContext), null);
            bleDevicesInfo.setValue(0);
            mDataList.get(dataListIndex).mBleDeviceHeartRateList.add(bleDevicesInfo);
            dataListIndex++;
            dataListTimeSecond = startTimeSecond + dataListIndex;
        }
    }

    private static void onCombineResult(int result, HciFileHeartRateFetcher.HciCombineCallback hciCombineCallback){
        if(null != hciCombineCallback){
            hciCombineCallback.onCombineResult(result);
        }
    }
}
